package com.yidu.sanrenxing.crudall.service.impl;

import com.yidu.sanrenxing.crudall.dao.UserAdressMapper;
import com.yidu.sanrenxing.crudall.domain.AdressMsg;
import com.yidu.sanrenxing.crudall.domain.UserAdress;
import com.yidu.sanrenxing.crudall.service.AdressMsgService;
import com.yidu.sanrenxing.crudall.service.UserAdressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class UserAdressServiceImpl implements UserAdressService {

    //地址信息表业务层bean
    @Autowired
    private AdressMsgService adressMsgService;

    @Autowired
    private UserAdressMapper userAdressMapper;

    @Override
    public List<UserAdress> selectAll() {
        return null;
    }

    @Override
    public int deleteByPrimaryKey(int useradressid) {
        return 0;
    }

    @Override
    public int insert(UserAdress record) {
        return 0;
    }

    @Override
    public int insertSelective(UserAdress record) {
        return userAdressMapper.insertSelective(record);
    }

    @Override
    public UserAdress selectByPrimaryKey(int useradressid) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(UserAdress record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(UserAdress record) {
        return 0;
    }

    /**
     * 通过用户编号查询该用户所需要的所有地址信息
     * @param userId
     * @return
     */
    @Override
    public List<Object> getShippingAdsMsgById(Integer userId) {
        List<UserAdress> shippingMsg = userAdressMapper.getShippingAdsMsgById(userId);
        ArrayList<Object> list = new ArrayList<>();
        for (UserAdress userAdress : shippingMsg) {
            HashMap<String, Object> map = new HashMap<>();
            //通过地址信息编号查询地址信息表数据
            AdressMsg adsMsg = adressMsgService.getAdsMsgById(userAdress.getAdressmsgid());
            map.put("adressMsgId",userAdress.getAdressmsgid());
            map.put("contName",adsMsg.getContname());
            map.put("contPhone",adsMsg.getContphone());
            map.put("areaAdress",adsMsg.getAreaadress().split(","));
            map.put("careAdress",adsMsg.getCareadress());
            map.put("selectStatus",userAdress.getSelectstatus());
            map.put("isNewAdd",false);
            map.put("ifLL",false);
            list.add(map);
        }
        return list;
    }

    /**
     * 修改用户地址状态
     * @param userAdress
     * @return
     */
    @Override
    public Integer updateSelectStatusByUserIdAndAdsId(UserAdress userAdress) {
        return userAdressMapper.updateSelectStatusByUserIdAndAdsId(userAdress);
    }

    @Override
    public Integer cancelAllSelectDefaultStatusByUserId(Integer userId) {
        return userAdressMapper.cancelAllSelectDefaultStatusByUserId(userId);
    }

    @Override
    public Integer deleteUserAdsMsgByAdrIdAndUserId(Integer adressmsgid, Integer userid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("adressMsgId",adressmsgid);
        map.put("userId",userid);
        return userAdressMapper.deleteUserAdsMsgByAdrIdAndUserId(map);
    }

    @Override
    public Boolean ifHaveDefaultSelectStatus(Integer userId) {
        return userAdressMapper.ifHaveDefaultSelectStatus(userId) > 0 ? false : true;
    }

    @Override
    public Integer updateFirstLineDataForDefaultSelect(Integer userId) {
        return userAdressMapper.updateFirstLineDataForDefaultSelect(userId);
    }
}
